<?php	
    session_start();
    if(session::get('access_token')=='')
    {
        $appid = "wxa14100eafc68daea";
        $appsecret = "52ba3eab3179000c68ccdeb4d90b21eb";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";

        $result = https_request($url);
        $jsoninfo = json_decode($result, true);
        session::set('access_token', $jsoninfo["access_token"], 7200);
    }
    else
    {
        $access_token = session::get('access_token');
        getInfo($access_token, "o8xb7wb_EPTTZcwaIX1xCR3wN1mI");
        exit;
        
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
        $result = https_request($url);
        $jsoninfo = json_decode($result);  // 默认false，为Object，若是True，为Array
        
        $data = $jsoninfo->data;
        $arr = $data->openid;  // 获得所有用户的Openid

        $temp = 0;
        while ($temp < count($arr))
        {
            $openid = $arr[$temp];
            getInfo($access_token, $openid);
            $temp++;
        }
    }


	// 根据Openid获取单个用户信息，如nickname
	function getInfo($access_token,$openid)
    {
		$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
		$output = https_request($url);
		$jsoninfo = json_decode($output);
        //var_dump($jsoninfo);
		//echo $jsoninfo -> headimgurl;
		echo $jsoninfo -> nickname;
        echo "<img src=".$jsoninfo -> headimgurl." height=\"50\" width=\"50\"></img>";
		echo "<br>";
	}
	 
	function https_request($url)
	{       
		$curl = curl_init();       
		curl_setopt($curl, CURLOPT_URL, $url);       
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);       
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);       
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);       
		$data = curl_exec($curl);       
		if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}       
		curl_close($curl);       
		return $data;
	}
    
    
/**
 * Session控制类
 */
class Session
{
    /**
     * 设置session
     * @param String $name   session name
     * @param Mixed  $data   session data
     * @param Int    $expire 超时时间(秒)
     */
    public static function set($name, $data, $expire = 600)
    {
        $session_data = array();
        $session_data['data'] = $data;
        $session_data['expire'] = time() + $expire;
        $_SESSION[$name] = $session_data;
    }

    /**
     * 读取session
     * @param  String $name  session name
     * @return Mixed
     */
    public static function get($name)
    {
        if (isset($_SESSION[$name]))
        {
            if ($_SESSION[$name]['expire'] > time())
            {
                return $_SESSION[$name]['data'];
            }
            else
            {
                self::clear($name);
            }
        }
        return false;
    }

    /**
     * 清除session
     * @param  String  $name  session name
     */
    private static function clear($name)
    {
        unset($_SESSION[$name]);
    }
}

?>